<?php
/**
 * Source code of php-gtk-ide Kernel
 * @category php-gtk-ide
 * @package core
 * @author Juan Benavides Romero <juan.benavides.romero@gmail.com>
 */
Kernel::import('core.exceptions');
Kernel::import('core.windows');

include('notepad_settings.class.php');
include('notepad_history.class.php');
include('notepad_filedrop.class.php');


/**
 * This function is a handler to load the class and function autmaticly.
 * Call a kernel function to load it.
 * @package core
 * @author Juan Benavides Romero <juan.benavides.romero@gmail.com>
 * @param string $functionName Name of the function of class to load
 */
function __autoload($functionName) {
    Kernel::instance()->autoload($functionName);
}//__autoload


/**
 * Kernel of the php-gtk-ide application
 * @category php-gtk-ide
 * @package core
 * @author Juan Benavides Romero <juan.benavides.romero@gmail.com>
 */
class Kernel {
	/**
	 * Instance of Kernel class uses to singleton pattern
	 * @var Kernel
	 */
	private static $instance;	
	


	/**
	 * Virtual namespace to load the functions and classes
	 * @var array
	 */
	private $importList;



	/**
	 * Application path
	 * @var string
	 */
	private $path;



	/**
	 * Loader window
	 * @var LoaderWindow
	 */
	private $loader;



	/**
	 * Ide window
	 * @var IdeWindow
	 */
	private $ide;



	/**
	 * Version of the puntoengine kernel. Indicate the version of puntoengine
	 */
	const VERSION = '0.1.0';



	/**
	 * Private constructor to singleton pattern
	 */	
	private function __construct() {
	}//__construct



	/**
	 * Return application path
	 * @return string Application path
	 */
	public function getPath() {
		return $this->path;
	}//getPath
	

	
	/**
	 * Return the singleton instance of Kernel
	 * @return Kernel Application Kernel
	 */
	public static function instance() {
		if(Kernel::$instance == null) {
			Kernel::$instance = new Kernel();
		}
		return Kernel::$instance;
	}//instance



	/**
	 * Add a import virtual path namespace to load the classes
	 * @param string $import Virtual namespace path to load
	 */
	public static function import($import) {
		Kernel::instance()->addImport($import);
	}//import



	/**
	 * Initialize the kernel application
	 * @param array $args Arguments params array
	 */
	public function process($args) {
		$this->configure();
		$this->loadingWindow();
		//$this->ideWindow();

		Gtk::main();
	}//process



	/**
	 * Load the loading window
	 * @param array $args Arguments params array
	 */
	public function loadingWindow() {
		$this->loader = new LoaderWindow();
		$this->loader->show_all();
		Gtk::timeout_add(1000, array($this->loader, 'loadAll'));
	}//loadingWindow



	/**
	 * Load the ide window
	 * @param array $args Arguments params array
	 */
	public function ideWindow() {
		$this->loader->destroy();

		$this->ide = new IdeWindow();
		$this->ide->show_all();
	}//ideWindow



	/**
	 * Load the error window
	 * @param array $args Arguments params array
	 */
	public function errorWindow(Exception $exception) {
		$dialog = new GtkMessageDialog(       
			new GtkWindow(),
			Gtk::DIALOG_MODAL | Gtk::DIALOG_DESTROY_WITH_PARENT,
			Gtk::MESSAGE_INFO,
			Gtk::BUTTONS_OK,
			print_r($exception, true)
		);
                
        $dialog->run();
		$dialog->destroy();
	}//errorWindow



	/**
	 * Configure the kernel of the php-gtk-ide
	 */
	private function configure() {
		set_error_handler(array($this, 'errorHandler'));
		set_exception_handler(array($this, 'exceptionHandler'));

		date_default_timezone_set('Europe/Madrid');
		
		$path = dirname(__FILE__);
		$path = str_replace('\\', '/', $path);
		$path = str_replace('/core','', $path);
		
		$this->path = $path.'/';
	}//configure
	


	/**
	 * Load automaticly the class and the function search this in the import
	 * list with the virtual paths
	 * @param string $functionName Name of the class or function to load
	 */
	public function autoload($functionName) {
		for($i = 0; $i < count($this->importList); $i++) {
			$dir = $this->path.'/'.str_replace('.', '/', $this->importList[$i]);
			$file = $dir.'/'.$functionName.'.php';

			if(is_file($file)) {
				include_once($file);
			}
		}
	}//autoload



	/**
	 * Add a import virtual path namespace to load the classes
	 * @param string $import Virtual namespace path to load
	 */
	public function addImport($import) {
		$this->importList[] = $import;
	}//addImport



	/*
	 * Catch a error and manage it with a internal exception manager
	 * @param int $code Error code
	 * @param string $message Descriptive error message
	 * @param string $file File where throw the error
	 * @param int $line Line where throw the error
	 * @param array $context Variables context when the error throwed
	 */
	public function errorHandler($code, $message, $file, $line, $context) {
		$exception = new CoreException(0, $message);

		$this->exceptionHandler($exception);
	}//errorHandler


	/**
	 * Catch a exception and manage it with a internal process
	 * @param Exception $exception Catched exception
	 */
	public function exceptionHandler(Exception $exception) {
		print_r($exception);
		$this->errorWindow($exception);
	}//exceptionHandler
}//Kernel
?>
